#include <stdio.h>

// 线性查找函数
int linear_search(int arr[], int size, int target)
{
    for (int i = 0; i < size; i++)
    { // 遍历数组
        if (arr[i] == target)
        {             // 如果找到目标元素
            return i; // 返回索引位置
        }
    }
    return -1; // 遍历结束未找到，返回-1
}

int main()
{
    int data[] = {12, 45, 7, 23, 56};          // 测试数组
    int size = sizeof(data) / sizeof(data[0]); // 计算数组长度

    // 情况1：查找成功
    int target1 = 23;
    int result1 = linear_search(data, size, target1);

    if (result1 != -1)
    {
        printf("Element %d found at index %d.\n", target1, result1); // 输出查找成功信息
    }
    else
    {
        printf("Element %d not found in the array.\n", target1);
    }

    // 情况2：查找失败
    int target2 = 99;
    int result2 = linear_search(data, size, target2);

    if (result2 != -1)
    {
        printf("Element %d found at index %d.\n", target2, result2);
    }
    else
    {
        printf("Element %d not found in the array.\n", target2); // 输出查找失败信息
    }

    return 0;
}